package gruppe7.util;

import gruppe7.control.ServletController;
import javax.servlet.http.HttpServletRequest;

public class Html {

    public String setHeader(String title) {
        String header = "<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'"
                + "'http://www.w3.org/TR/html4/loose.dtd'><html><head><meta http-equiv='Content-Type'"
                + "content='text/html; charset=iso-8859-1'><link href='style.css' rel='stylesheet'"
                + "type='text/css' /><title>Gruppe7 - " + title + "</title></head>";
        return header;
    }

    public String setFooter() {
        String footer = "<div id ='footer'><p>Portfolio Exercise and Exam Project Spring 2010 - "
                + "gruppe 7 - Thomas Slott - Daniel Stentsøe - Jacob Vous - Peter Holtegaard - Version 3</p></div></body></html>";
        return footer;
    }

    public String LoginFormString(HttpServletRequest req, boolean error) {
    	ServletController sc = ServletController.getInstance();
    	
    	String username = sc.getUsernameCookieValue(req);
        String password = sc.getPasswordCookieValue(req);
        
        if (username == null) username = "";
        if (password == null) password = "";
        
        String userCheckbox = String.format("<input class='radio' type='checkbox' %s name='remUser'/>", username.isEmpty() ? "" : "checked");
        String pwCheckbox = String.format("<input class='radio' type='checkbox' %s name='remPw'/>", password.isEmpty() ? "" : "checked");
    	
    	String errorMsg = error ? "<p style='color:red'>Invalid username or password</p>" : "";
        
        String form = "<table class='default'>" + errorMsg
                + "<form action='realm_cookie.do' method='post'>"
                + "<tr><td>Username</td><td><input type='text' name='j_username' value='" + username + "'/>"
                + "</td><td>" + userCheckbox + "<label for='remUser'>Remember Username?</label></td></tr>"
                + "<tr><td>Password</td><td><input type='password' name='j_password' value='" + password + "'/></td>"
                + "<td>" + pwCheckbox + "<label for='remPw'>Remember Password?</label></td></tr>"
                + "<tr><td></td><td align='right'><input class='submit' type='submit' value='Login'/></td></tr>"
                + "</form>"
                + "</table>";

        return form;
    }
}

